package com.fh.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fh.common.web.impl.AbstractServiceImpl;
import com.fh.modules.ems.service.IEmsCouponService;
import com.fh.modules.ums.entity.UmsMember;
import com.fh.modules.ums.entity.UmsMemberPartnerInfo;
import com.fh.modules.ums.entity.UmsPartnerApply;
import com.fh.modules.ums.entity.UmsPartnerLevel;
import com.fh.modules.ums.mapper.UmsMemberMapper;
import com.fh.modules.ums.model.MemberModel;
import com.fh.modules.ums.model.MemberSimpleInfo;
import com.fh.modules.ums.model.PartnerModel;
import com.fh.modules.ums.service.IUmsMemberPartnerInfoService;
import com.fh.modules.ums.service.IUmsMemberService;
import com.fh.modules.ums.service.IUmsPartnerLevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description: 商城会员
 * @author： code generator
 * @date： 2019-09-25
 * @version： V1.0
 */
@Service
public class UmsMemberServiceImpl extends AbstractServiceImpl<UmsMemberMapper, UmsMember> implements IUmsMemberService {

    @Autowired
    private IUmsMemberPartnerInfoService umsMemberPartnerInfoService;
    @Autowired
    private IUmsPartnerLevelService umsPartnerLevelService;
    @Autowired
    private IEmsCouponService emsCouponService;

    @Override
    public void addPartnerInfo(UmsPartnerApply umsPartnerApply) {
        UmsMember umsMember=this.getById(umsPartnerApply.getMemberId());
        if(umsMember.getPartnerType()!=1){
            UmsMemberPartnerInfo umsMemberPartnerInfo=new UmsMemberPartnerInfo();
            BigDecimal initZero=new BigDecimal(0);
            umsMemberPartnerInfo.setGrowth(initZero);
            umsMemberPartnerInfo.setTeamPersonCount(1);
            umsMemberPartnerInfo.setTeamHistorySales(initZero);
            umsMemberPartnerInfo.setPersonCommission(initZero);
            umsMemberPartnerInfo.setPersonCommissionHistory(initZero);
            umsMemberPartnerInfo.setId(umsPartnerApply.getMemberId());
            umsMemberPartnerInfo.setMemberLevelId(umsPartnerLevelService.getDefaultPartnerLevel().getId());
            umsMemberPartnerInfo.setPartnerCode(umsMemberPartnerInfoService.getNewPartnerCode());
            umsMemberPartnerInfoService.save(umsMemberPartnerInfo);
            umsMember.setPartnerType(1);
            umsMember.setPhone(umsPartnerApply.getMobile());
            umsMember.setUsername(umsPartnerApply.getName());
            umsMember.setPid(umsPartnerApply.getPid());
            umsMember.setEmail(umsPartnerApply.getEmail());
            umsMember.setAddress(umsPartnerApply.getAddress());
            this.updateById(umsMember);
        }
    }

    @Override
    public MemberModel getMemberInfo(Integer memberId) {
        UmsMember umsMember = this.getById(memberId);
        return this.getMemberInfo(umsMember);
    }

    @Override
    public MemberSimpleInfo querySimpleInfo(Integer memberId) {
        MemberSimpleInfo memberSimpleInfo=new MemberSimpleInfo();
        UmsMember umsMember=this.getById(memberId);
        memberSimpleInfo.copyProperty(umsMember);
        if(umsMember.getPartnerType()==1){
            UmsMemberPartnerInfo umsMemberPartnerInfo=umsMemberPartnerInfoService.getById(memberId);
            memberSimpleInfo.copyProperty(umsMemberPartnerInfo);
            UmsPartnerLevel umsPartnerLevel= umsPartnerLevelService.getById(umsMemberPartnerInfo.getMemberLevelId());
            memberSimpleInfo.setPartnerLevelId(umsPartnerLevel.getId());memberSimpleInfo.setPartnerLevelname(umsPartnerLevel.getName());
        }
        //查询用户拥有的未使用的优惠券数量
        memberSimpleInfo.setCouponCount(emsCouponService.queryAvailableCouponCount(memberId));
        return memberSimpleInfo;
    }

    @Override
    public MemberModel getMemberInfo(UmsMember umsMember){
        if(umsMember==null){
            return null;
        }
        MemberModel memberModel = new MemberModel();
        memberModel.setPartner(umsMember.getPartnerType()==1?true:false);
        memberModel.setMemberInfo(umsMember);
        Integer partnerId = umsMember.getPrePartnerId();
        //当前用户不是合伙人，且他有上级合伙人则执行下面流程
        if (partnerId != null&&umsMember.getPartnerType() != 1 ) {
            memberModel.setPartnerInfo(this.getPartnerModel(partnerId));
        }
        if(umsMember.getPartnerType()==1){
            memberModel.setUmsMemberPartnerInfo(umsMemberPartnerInfoService.getById(umsMember.getId()));
        }
        return memberModel;
    }

    @Override
    public PartnerModel getPartnerModel(Integer memberId) {
        UmsMember partner = this.getById(memberId);
        if(partner.getPartnerType()!=1){
            return null;
        }
        return this.getPartnerModel(partner);
    }

    @Override
    public MemberModel getMemberModelByOpenId(String openId) {
        LambdaQueryWrapper<UmsMember> query=new LambdaQueryWrapper<UmsMember>();
        query.eq(UmsMember::getThirdAccount,openId);
        UmsMember umsMember=this.getOne(query);
        return this.getMemberInfo(umsMember);
    }

    @Override
    public PartnerModel getPartnerModelByPartnerCode(String partnerCode) {
        UmsMemberPartnerInfo umsMemberPartnerInfo=umsMemberPartnerInfoService.getUmsMemberPartnerInfoByPartnerCode(partnerCode);
        return umsMemberPartnerInfo==null?null:this.getPartnerModel(umsMemberPartnerInfo.getId());
    }

    @Override
    public PartnerModel getPartnerModel(UmsMember partner) {
        UmsMemberPartnerInfo partnerInfo = umsMemberPartnerInfoService.getById(partner.getId());
        if (partner != null && partnerInfo != null) {
            PartnerModel partnerModel = new PartnerModel();
            partnerModel.setMemberInfo(partner);
            partnerModel.setOwnPartnerInfo(partnerInfo);
            partnerModel.setPartnerLevel(umsPartnerLevelService.getById(partnerInfo.getMemberLevelId()));
            return partnerModel;
        }
        return null;
    }

    @Override
    public void addMemberConsumptionInformation(Integer integration, BigDecimal consumption, UmsMember umsMember) {
        umsMember.setHistoryIntegration(umsMember.getHistoryIntegration() + integration);
        umsMember.setCurrentIntegration(umsMember.getCurrentIntegration() + integration);
        umsMember.setConsumption(umsMember.getConsumption().add(consumption));
        umsMember.setHasBuyGood(1);
        this.updateById(umsMember);
    }

    @Override
    public void addParnterTeamSale(BigDecimal consumption,BigDecimal productMoney, Integer memberId) {
        UmsMemberPartnerInfo umsMemberPartnerInfo = new UmsMemberPartnerInfo();
        this.addParnterTeamSale(consumption,productMoney, umsMemberPartnerInfo);
    }

    @Override
    public void addParnterTeamSale(BigDecimal consumption,BigDecimal productMoney, UmsMemberPartnerInfo umsMemberPartnerInfo) {
        umsMemberPartnerInfo.setTeamHistorySales(umsMemberPartnerInfo.getTeamHistorySales().add(productMoney));
        umsMemberPartnerInfo.setGrowth(umsMemberPartnerInfo.getGrowth().add(productMoney));
        umsMemberPartnerInfo.setPersonCommission(umsMemberPartnerInfo.getPersonCommission().add(consumption));
        umsMemberPartnerInfo.setPersonCommissionHistory(umsMemberPartnerInfo.getPersonCommissionHistory().add(consumption));
        umsMemberPartnerInfoService.updateById(umsMemberPartnerInfo);
    }

    @Override
    public UmsMemberPartnerInfo getPartnerInfo(Integer memberId) {
        return umsMemberPartnerInfoService.getById(memberId);
    }
}
